package com.mhz.cae.hud;

import org.anddev.andengine.engine.camera.hud.HUD;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.content.Context;

import com.mhz.cae.events.Event;
import com.mhz.cae.events.Event.EventType;
import com.mhz.cae.events.EventManager;

public class OpenInventoryButton {

	// The object coordonates
	private final float mX;
	private final float mY;

	// The object textures
	private static TextureRegion mButtonTextureRegion;

	// Animated Sprite and body
	private final Sprite mFace;

	private OpenInventoryButton(final float pX, final float pY) {
		this.mX = pX;
		this.mY = pY;
		this.mFace = new Sprite(mX, mY, mButtonTextureRegion) {
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				switch (pSceneTouchEvent.getAction()) {
				case TouchEvent.ACTION_DOWN:
					EventManager.getInstance().pushEvent(new Event(EventType.OPEN_INVENTORY_EVENT));
					break;
				}
				return true;
			}
		};
	}

	public static OpenInventoryButton createButton(final float pX, final float pY) {
		return new OpenInventoryButton(pX, pY);
	}

	public static void loadResource(final Context context, final BitmapTextureAtlas bitmapTextureAtlas) {
		if (mButtonTextureRegion == null) {
			mButtonTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(bitmapTextureAtlas //
					, context //
					, "add_48.png" //
					, 48, 0);
		}
	}

	public void init(final HUD pHUD) {
		pHUD.attachChild(this.mFace);
		pHUD.registerTouchArea(this.mFace);
	}

}
